package com.metagurukul.metaspace.actions;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.metagurukul.metaspace.controller.StatusUpdateForm;
import com.metagurukul.metaspace.models.beans.Status;
import com.metagurukul.metaspace.service.StatusCommentService;

public class StatusUpdateAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws SQLException {
		StatusUpdateForm statusUpdateForm = (StatusUpdateForm) form;
		String status = statusUpdateForm.getStatus();
		HttpSession session = request.getSession();
		int userId = (Integer) session.getAttribute("id");

		if (insertStatus(userId, status) < 1) {
				ActionMessages errors = new ActionMessages();
				errors.add(
						"statusErr",
						new ActionMessage(
								"com.metagurukul.metaspace.status.error.connectionNotCreated"));
				saveErrors(request, errors);
			}
		return mapping.findForward("homePage");
	}
	
	public int insertStatus(int userId , String statusDesc) throws SQLException{
		Status status = new Status();
		status.setStatus(statusDesc);
		status.setUserId(userId);
		return StatusCommentService.addStatus(status);
	}
	
}
